package com.airsystem.pos.bengkel.entity;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.ForeignKey;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */

@Entity
@Table(name = "tbl_detail_pesan_sparepart")
public class DetailPesanSparepart implements Serializable {

	@Transient
	public static final String ALIAS = "detail_pesan_sparepart";

	@Id
	@GeneratedValue
	private Long id;

	private Long stok;

	private BigDecimal harga;

	private String distributor;

	private int metode;

	@ManyToOne
	@JoinColumn(name = "pesan_sparepart", nullable = false)
	@ForeignKey(name = "FK_PESAN_SPAREPART")
	private PesanSparepart pesan_sparepart;

	@ManyToOne
	@JoinColumns({ @JoinColumn(name = "sub_merk", referencedColumnName = "sub_merk"),
				   @JoinColumn(name = "detail_kendaraan", referencedColumnName = "detail_kendaraan") })
	@ForeignKey(name = "FK_DETAIL_SPAREPART_PESAN")
	private DetailSparepart detail_sparepart;

	public boolean equals(Object object) {
		if (!(object instanceof DetailPesanSparepart)) {
			return super.equals(object);
		}

		DetailPesanSparepart target = (DetailPesanSparepart) object;

		boolean objectJenisSparepart = this.getDetail_sparepart().getJenis_sparepart().getId()
										   .equals(target.getDetail_sparepart().getJenis_sparepart().getId());

		boolean objectSparepart = this.getDetail_sparepart().getSparepart().getMerk()
									  .equals(target.getDetail_sparepart().getSparepart().getMerk());

		boolean objectDetailSparepart = this.getDetail_sparepart().getSub_merk()
											.equals(target.getDetail_sparepart().getSub_merk());

		boolean isEqual = objectJenisSparepart && objectSparepart && objectDetailSparepart;

		return isEqual;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getStok() {
		return stok;
	}

	public void setStok(Long stok) {
		this.stok = stok;
	}

	public BigDecimal getHarga() {
		return harga;
	}

	public void setHarga(BigDecimal harga) {
		this.harga = harga;
	}

	public String getDistributor() {
		return distributor;
	}

	public void setDistributor(String distributor) {
		this.distributor = distributor;
	}

	public int getMetode() {
		return metode;
	}

	public String getMetode_s() {
		return Metode.get(metode).name();
	}

	public Metode getMetode_e() {
		return Metode.get(metode);
	}

	public void setMetode(String metode) {
		setMetode(Metode.get(metode));
	}

	public void setMetode(Metode metode) {
		this.metode = metode.getFlag();
	}

	public enum Metode {
		TUNAI(0),

		HUTANG(1),

		BOTH(2),

		UNKNOWN(-1);

		int flag;

		Metode(int flag) {
			this.flag = flag;
		}

		public int getFlag() {
			return flag;
		}

		public static Metode get(String metode) {
			for (Metode item : values()) {
				if (item.name().equalsIgnoreCase(metode.trim())) {
					return item;
				}
			}

			return UNKNOWN;
		}

		public static Metode get(int metode) {
			for (Metode item : values()) {
				if (item.getFlag() == metode) {
					return item;
				}
			}

			return UNKNOWN;
		}
	}

	public PesanSparepart getPesan_sparepart() {
		return pesan_sparepart;
	}

	public void setPesan_sparepart(PesanSparepart pesan_sparepart) {
		this.pesan_sparepart = pesan_sparepart;
	}

	public DetailSparepart getDetail_sparepart() {
		return detail_sparepart;
	}

	public void setDetail_sparepart(DetailSparepart detail_sparepart) {
		this.detail_sparepart = detail_sparepart;
	}
}